//author: create by SunJH
//时间: 2021/12/3 0003
//邮箱: 965989158@qq.com
//描述: 上拉刷新，下拉加载更多的库测试工程

import 'package:card_swiper/card_swiper.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app/counter/sliver_controller.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:get/get.dart';
import 'package:get/get_state_manager/src/simple/get_view.dart';

class RefreshDart extends GetView<SliverController> {
   RefreshDart({Key? key}) : super(key: key);
   var refController = EasyRefreshController();
   @override
  Widget build(BuildContext context) {
    Get.put<SliverController>(SliverController());
    return Scaffold(
      body: createRefPage(),
    );
  }

  createListView() {
    return Obx(()=>SliverList(
      delegate: SliverChildListDelegate(controller.infos.value
          .map((e) => Container(
          height: 120,
          alignment: Alignment.center,
          child: Text(
            e,
            style: TextStyle(fontSize: 20),
          )))
          .toList()),
    ));
  }

  bannerWidget() {
    return SliverToBoxAdapter(
      child: Obx(()=>SizedBox(
        height: 120,
        child: Swiper(
          itemCount: controller.bannerItems.length,
          pagination: const SwiperPagination(),
          controller: SwiperController(),
          loop: true,
          autoplay: true,
          itemBuilder: (context, index) {
            return Image.network(
              controller.bannerItems.value[index],
              fit: BoxFit.cover,
            );
          },
        ),
      )),
    );
  }

  createTopInfoWidget() {
    return SliverToBoxAdapter(
      child: Stack(
        children: [
          Container(
            padding: const EdgeInsets.only(right: 30),
            alignment: Alignment.centerRight,
            child: Image.network(
              controller.topWeatherPath.value,
              width: 40,
              height: 40,
            ),
          ),
          Padding(
            padding: const EdgeInsets.only(left: 20),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: const [
                Text(
                  "学校管理员,上午好",
                  style: TextStyle(fontSize: 18),
                ),
                Text(
                  "2021年11月26日，星期五",
                  style: TextStyle(fontSize: 15),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
   createLoadingView() {
     return SliverToBoxAdapter(
       child: Obx((){
         return controller.isLoadMore.value?Column(
           mainAxisAlignment: MainAxisAlignment.center,
           children: const [
             CircularProgressIndicator(valueColor: AlwaysStoppedAnimation(Colors.blue),strokeWidth: 4,),
             Text("加载中"),
           ],
         ):const SizedBox(height: 1,width: 1,);
       }),
     );
   }
  Widget createRefPage() {
    return RefreshIndicator(
      onRefresh: ()async{
        await Future.delayed(Duration(seconds: 3),(){});
      },
      child: CustomScrollView(
        physics: const NeverScrollableScrollPhysics(),
        controller: controller.scrollController,
        slivers: [
          SliverToBoxAdapter(
            child: Container(
              height: 100,
            ),
          ),
          createTopInfoWidget(),
          bannerWidget(),
          createSliverGrid(),
          createListView(),
          createLoadingView(),
        ],
      ),
    );
  }
  createRefresh(){
    return EasyRefresh.custom(
      controller: refController,
      enableControlFinishRefresh: true, //开启下拉刷新
      enableControlFinishLoad: true,    //开启上拉加载更多
      header:  ClassicalHeader(
        enableInfiniteRefresh: false,   //是否无限下拉刷新
        bgColor: Colors.white,
        float: true,                    //header是否浮在界面上
        enableHapticFeedback: true,     //是否开启震动反馈
        refreshText: "刷新中",
        refreshReadyText: "等待刷新进行",
        refreshingText: "请稍等...",
        refreshedText: "刷新成功",
        refreshFailedText: "刷新失败",
        noMoreText: "没有更多数据了",
        infoText: "更新于:${DateTime.now()}",//额外信息
      ),
      footer: ClassicalFooter(
        enableHapticFeedback: true,             //是否开启震动反馈
        loadText: "加载更多",
        loadReadyText: "正在准备加载数据",
        loadingText: "加载中...",
        loadedText: "加载完成",
        loadFailedText: "加载失败",
        noMoreText: "没有更多数据",
        infoText: "上次刷新时间:${DateTime.now()}", //额外信息
      ),
      onRefresh:() async {
        Future.delayed(const Duration(seconds: 3),(){
          refController.finishRefresh();
          refController.resetLoadState();
        });
      },
      onLoad:() async {
        Future.delayed(const Duration(seconds: 3),(){
          refController.finishLoad(noMore: true);
        });
      },
      slivers: [
        SliverToBoxAdapter(
          child: Container(
            height: 100,
          ),
        ),
        createSpace(40),
        createTopInfoWidget(),
        createSliverGrid(),
        createSpace(20, color: Colors.grey.shade300),
        createListView(),
      ],
    );
  }
  createSliverGrid() {
    return Obx(()=>SliverGrid(
      delegate: SliverChildBuilderDelegate(
            (context, index) {
          var value = controller.datas[index];
          return Container(
            color: Colors.red,
            alignment: Alignment.center,
            child: Text(value.toString(),style: const TextStyle(color: Colors.white,fontSize: 20),),
          );
        },
        childCount: controller.datas.length,
      ),
      gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
          crossAxisCount: 5,
          mainAxisSpacing: 1,
          crossAxisSpacing: 1,
          childAspectRatio: 1.2),
    ));
  }
   createSpace(double height, {Color? color}) {
     return SliverToBoxAdapter(
       child: Container(
         color: color,
         height: height,
       ),
     );
   }
}
